﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Programa_Programas : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        //ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "$.modaldialog.success('El programa ha sido registrado', {timeout: 2 , showClose: false });", true);

        
    
        if (Session["nit"] != null)
            Response.Redirect("/SUEWebSite/Error.aspx");
        //-------------------------Lineas Agregadas para la seguridad----------------------------------------------------
        else if (Session["nomUsr"] != null)
        {
            List<String> Permisos = (List<String>)Session["Permisos"];

            if (!Permisos.Contains(System.IO.Path.GetFileName(HttpContext.Current.Request.FilePath)))
                Response.Redirect("/SUEWebSite/Error.aspx");
            else
            {
                DropDownListProgramas.Visible = false;
                ButtonRegPrograma.Visible = false;
                ButtonElim.Visible = false;
                ButtonNo.Visible = false;
                ButtonSi.Visible = false;
                Notificacion.Visible = false;
                ButtonMod.Visible = false;
                TextBoxModSE.Visible = false;
                ButtonModGuardar.Visible = false;
                ButtonModCancelar.Visible = false;
                Label1.Text = "Nombre";  
                
            }

        }
        else Response.Redirect("/SUEWebSite/Principal.aspx");

        //-------------------------Lineas Agregadas para la seguridad----------------------------------------------------
        
      
    }

    private void CargarListProgramas()
    {
        DropDownListProgramas.Items.Clear();
        Programa prog = new Programa();
        List<Programa> listProg = prog.consultarProgramas();

        foreach (Programa pro in listProg)
        {
            DropDownListProgramas.Items.Add(pro.ObtNombre());
        }

    }



    protected void ButtonRegProg_Click(object sender, EventArgs e)
    {
        ButtonBuscar.Visible = true;
        TextBoxNombreSec.Visible = true;
        ButtonReg.Visible = true;
    }
    protected void ButtonBuscar_Click(object sender, EventArgs e)
    {
        CargarListProgramas();
        ButtonRegPrograma.Visible = true;
        ButtonBuscar.Visible = false;
        DropDownListProgramas.Visible = true;
        TextBoxNombreSec.Visible = false;
        ButtonReg.Visible = false;
        ButtonElim.Visible = true;
        ButtonMod.Visible = true;
    }
    protected void ButtonElim_Click(object sender, EventArgs e)
    {

        Notificacion.Visible = true;
        Label1.Text = "";
        Notificacion.Text = "Desea eliminar el programa " + DropDownListProgramas.SelectedItem.ToString() + " ?";
        ButtonSi.Visible = true;
        ButtonNo.Visible = true;
    }
    protected void ButtonReg_Click(object sender, EventArgs e)
    {
        Notificacion.Visible = true;
        Notificacion.Text = "";
        CargarListProgramas();
        
        if (TextBoxNombreSec.Text != "")
        {
            if (DropDownListProgramas.Items.FindByValue(TextBoxNombreSec.Text.ToLower()) == null)
            {
                Programa prog = new Programa(TextBoxNombreSec.Text.ToLower());
                prog.CrearPrograma();
                TextBoxNombreSec.Text = "";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "$.modaldialog.success('El programa ha sido registrado', {timeout: 2 , showClose: false });", true);
                
            }
            else
                ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "$.modaldialog.error('El programa ya existe', {timeout: 2 , showClose: false });", true);
        }
        else Notificacion.Text = "El campo es obligatorio";

    }


    protected void ButtonSi_Click(object sender, EventArgs e)
    {
        Programa prog = new Programa();
        Notificacion.Visible = true;
        if (prog.EliminarPrograma(DropDownListProgramas.SelectedItem.ToString()) == true)
        {
            TextBoxNombreSec.Text = "";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "$.modaldialog.success('El programa ha sido eliminado', {timeout: 2 , showClose: false });", true);
           
            cargarPrincipal();
        }
        else
            ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "$.modaldialog.warning('No se puede eliminar este programa, porque tiene datos de perfiles asociados', {timeout: 2 , showClose: false });", true);
        Notificacion.Visible = false;   
        cargarPrincipal();

    }

    private void cargarPrincipal()
    {
        TextBoxNombreSec.Visible = true;
        ButtonReg.Visible = true;
        ButtonBuscar.Visible = true;
    }

    protected void ButtonNo_Click(object sender, EventArgs e)
    {
        cargarPrincipal();

    }
    protected void ButtonMod_Click(object sender, EventArgs e)
    {
        cargarMod();
    }
    protected void ButtonModCancelar_Click(object sender, EventArgs e)
    {
        cargarPrincipal();
    }
    protected void ButtonModGuardar_Click(object sender, EventArgs e)
    {
        Notificacion.Visible = true;
        Notificacion.Text = "";
        
        if (TextBoxModSE.Text != "")
        {
            if (DropDownListProgramas.Items.FindByValue(TextBoxModSE.Text.ToLower()) == null)
            {
                Programa prog = new Programa();

                if (prog.ActualizarPrograma(DropDownListProgramas.SelectedItem.ToString(), TextBoxModSE.Text.ToLower()) == true)
                {
                    TextBoxModSE.Text = "";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "$.modaldialog.success('El programa ha sido actualizado', {timeout: 2 , showClose: false });", true);
                 
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "$.modaldialog.warning('No se puede modificar este programa, porque tiene datos de perfiles asociados', {timeout: 2 , showClose: false });", true);
                }

                Notificacion.Visible = false;
                //cargarPrincipal();


            }
            else
                ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "$.modaldialog.error('El programa ya existe', {timeout: 2 , showClose: false });", true);
                cargarPrincipal();

            

        }
        else
        {
            cargarMod();
            Notificacion.Text = "Debe ingresar un nombre para " + DropDownListProgramas.SelectedItem.ToString();

        }

    }

    private void cargarMod()
    {
        ButtonModGuardar.Visible = true;
        ButtonModCancelar.Visible = true;
        TextBoxModSE.Visible = true;
        Label1.Text = "";
        Notificacion.Visible = true;
        Notificacion.Text = "Ingrese el nuevo nombre para el porgrama " + DropDownListProgramas.SelectedItem.ToString();
    }

    public void MessageBoxJS(string msg)
    {
        StringBuilder str = new StringBuilder("<script languaje = JavaScript>");
        str.Append("alert('" + msg + "');");
        str.Append("</script>");
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "PopUpScript", str.ToString());


    }


   
}
